package net.xiduth.game.player.controlers;

import net.xiduth.Settings;
import net.xiduth.game.Entity;
import net.xiduth.game.World;
import net.xiduth.game.WorldObject;
import net.xiduth.game.WorldTile;
import net.xiduth.game.item.Item;
import net.xiduth.game.npc.NPC;
import net.xiduth.game.player.Player;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;
import net.xiduth.utils.Misc;

public class StartTutorial extends Controler {

	private static final int QUEST_GUIDE_NPC = 945;

	private static final String[][] TUTORIAL_MESSAGES = {
			{
					"Welcome to Xiduth",
					"We welcome you with open arms, and hope you enjoy your stay.",
					"Welcome to Lumbridge. Walk to the general store and",
					"buy a varrock teleport tablet...", "", "", "" },
			{ "Xiduth Tutorial",
					"In your inventory, break your varrock teleport tablet",
					"", "", "", "", "" },
			{
					"Xiduth Tutorial",
					"Press your magic tab and click on the home teleport.",
					"Choose varrock teleport by clicking on it, once you are",
					"at the varrock lodestone, you will be at the next stage in the tutorial.",
					"", "", "" },
			{ "Talk to the Quest Guide",
					"Walk back to the centre of Varrock and engage",
					"in a conversation with the RuneScape guide",
					"by left clicking on anywhere on his body.", "", "", "" },
					{ "Customize your character", "Go over to Thessilia and customize the appearance",
					"of your character. You can do this by right clicking her and ",
					"pressing \"Change-Clothes\".", "", "", ""}};

	private final Item[] starterItems = new Item[] { new Item(1856, 1),
			new Item(556, 100), new Item(558, 50), new Item(1321, 1),
			new Item(841, 1), new Item(884, 500) };

	/**
	 * 	player.getInventory().addItem(1856, 1); // Information Book
			player.getInventory().addItem(556, 100); // Air Rune
			player.getInventory().addItem(558, 50); // Mind Rune
			player.getInventory().addItem(1321, 1); // Bronze Scimitar
			player.getInventory().addItem(841, 1); // Shortbow
			player.getInventory().addItem(884, 500); // Iron Arrow
	 * 
	 */
	
	@Override
	public void start() {
		player.setYellOff(true);
		refreshStage();
		player.getPackets().sendMusicEffect(13);
	}

	public NPC findNPC(int id) {
		for (NPC npc : World.getNPCs()) {
			if (npc == null || npc.getId() != id)
				continue;
			return npc;
		}
		return null;
	}

	@Override
	public boolean processButtonClick(int interfaceId, int componentId, int slotId, int packetId) {
		if (interfaceId == 679 && packetId == 6) {
			player.getPackets().sendGameMessage("You have no reason to drop items during the tutorial.");
			return false;
		}
		if (interfaceId == 1092) {
			if (componentId == 51) {
				return true;
			} else {
				player.getPackets().sendGameMessage("Please teleport to Varrock.");
				return false;
			}
		}
		return true;
	}

	@Override
	public void process() {
		refreshStage();
	}

	@Override
	public boolean processObjectClick1(WorldObject object) {
		return false;
	}

	@Override
	public boolean processObjectClick2(WorldObject object) {
		return false;
	}

	@Override
	public boolean processObjectClick3(WorldObject object) {
		return false;
	}

	public void refreshStage() {
		int stage = getStage();
		if (stage == 0) {
			NPC guide = findNPC(522);
			if (guide != null)
				player.getHintIconsManager().addHintIcon(guide, 0, -1, false);
		} else if (stage == 1) {
			player.getHintIconsManager().removeUnsavedHintIcon();
			player.getPackets().sendConfig(1021, 22);
			player.getPackets().sendConfig(1021, 5);
		} else if (stage == 2) {
			player.getHintIconsManager().removeUnsavedHintIcon();
			player.getPackets().sendConfig(1021, 22);
			player.getPackets().sendConfig(1021, 0);
		} else if (stage == 3) {
			NPC guide = findNPC(QUEST_GUIDE_NPC);
			if (guide != null)
				player.getHintIconsManager().addHintIcon(guide, 0, -1, false);
		} else if (stage == 4) {
			player.getHintIconsManager().removeUnsavedHintIcon();
			NPC target = findNPC(548);
			if (target != null)
				player.getHintIconsManager().addHintIcon(target, 0, -1, false);
		}
		sendInterfaces();
	}

	@Override
	public void sendInterfaces() {
		int stage = getStage();
		player.getInterfaceManager().replaceRealChatBoxInterface(372);
		if (stage < 5) {
			for (int j = 0; j < TUTORIAL_MESSAGES[stage].length; j++)
				player.getPackets().sendIComponentText(372, j, TUTORIAL_MESSAGES[stage][j]);
		} else 
			forceClose();
	}

	public void updateProgress() {
	}

	@Override
	public boolean processNPCClick1(NPC npc) {
		return (npc.getId() == QUEST_GUIDE_NPC || npc.getId() == 522);
	}

	@Override
	public boolean processNPCClick2(NPC npc) {
		if (npc.getDefinitions().name.toLowerCase().contains("shop") && getStage() == 0
				|| npc.getId() == 548) {
			return true;
		}
		return false;
	}

	public void setStage(int stage) {
		getArguments()[0] = stage;
	}

	public int getStage() {
		if (getArguments() == null)
			setArguments(new Object[] { 0 }); // index 0 = stage
		return (Integer) getArguments()[0];
	}

	@Override
	public boolean login() {
		start();
		return false;
	}

	@Override
	public boolean logout() {
		return false;
	}

	@Override
	public boolean processMagicTeleport(WorldTile toTile) {
		return true;
	}

	@Override
	public boolean keepCombating(Entity target) {
		return false;
	}

	@Override
	public boolean canAttack(Entity target) {
		return false;
	}

	@Override
	public boolean canHit(Entity target) {
		return false;
	}

	@Override
	public boolean processItemTeleport(WorldTile toTile) {
		return true;
	}

	@Override
	public boolean processObjectTeleport(WorldTile toTile) {
		return false;
	}

	@Override
	public void forceClose() {
		removeControler();
		player.getHintIconsManager().removeUnsavedHintIcon();
		player.getMusicsManager().reset();
		player.setYellOff(false);
		for (Item item : starterItems) {
			if (item == null) continue;
			player.getInventory().addItem(item);
		}
		player.getPackets().sendGameMessage(
				"Congratulations! You finished the start tutorial.");
		player.getPackets().sendGameMessage("or talk with other players.");
		for (Player pl : World.getPlayers()) {
			if (pl == null) continue;
			pl.getPackets()
					.sendGameMessage(
							"<col=FFFFFF>"
									+ Misc.formatPlayerNameForDisplay(player
											.getUsername())
									+ "</col> <col=990066>has completed the tutorial! Welcome them to "
									+ Settings.SERVER_NAME + "</col>", true);
		}
		WorldTasksManager.schedule(new WorldTask() {
			@Override
			public void run() {
				player.getInterfaceManager().sendInterfaces();
				player.getInterfaceManager()
				.closeReplacedRealChatBoxInterface();
			}
		});
	}
}
